package com.shyb.lingxian.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.shyb.lingxian.R;
import com.shyb.lingxian.Util.AppUtil;

/**
 * Created by Administrator on 2018/3/22/022.
 */

public abstract class BaseActivity<P extends BasePresenter> extends AppCompatActivity {

    protected abstract P initPresenter();
    protected abstract void initialize();
    protected P presenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        presenter = initPresenter();
        initialize();
    }


    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams.setMargins(0, AppUtil.dp2px(this, 44), 0, 0);

        addContentView(getLayoutInflater().inflate(layoutResID, null), layoutParams);
        addContentView(getLayoutInflater().inflate(R.layout.include_head_view, null), new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, AppUtil.dp2px(this, 44)));

    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        super.setContentView(view, params);
    }

    /**
     * 设置标题
     *
     * @param title
     */
    protected void initHeadTitle(String title) {

        TextView tv_head_mid = (TextView) findViewById(R.id.tv_head_mid);
        if (tv_head_mid != null) {
            tv_head_mid.setText(title);
        }
    }


    protected void intHeadRight(String text){
        TextView viewById = (TextView) findViewById(R.id.tv_head_right);
        viewById.setVisibility(View.VISIBLE);
        viewById.setText(text);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) presenter.dettachView();
    }

}
